package equipment;

import impsoft.bots.reflection.Player;
import impsoft.scripting.ibot.itemrec.ItemCondition;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import looting.LootItem;
import looting.LootTable;
import base.DreamScript;
import base.DreamUtility;

public class EquipmentUtils extends DreamUtility {

	public static final int HELMET_SLOT = 0;
	public static final int CAPE_SLOT = 1;
	public static final int AMULET_SLOT = 2;
	public static final int WEAPON_SLOT = 3;
	public static final int BODY_SLOT = 4;
	public static final int SHIELD_SLOT = 5;
	public static final int LEGGING_SLOT = 7;
	public static final int GLOVE_SLOT = 9;
	public static final int BOOT_SLOT = 10;
	public LootTable masterLootTable = new LootTable();
	public Field playerDefField = null;

	public static int[] equipment = { HELMET_SLOT, CAPE_SLOT, AMULET_SLOT, WEAPON_SLOT, BODY_SLOT, SHIELD_SLOT,
			LEGGING_SLOT, GLOVE_SLOT, BOOT_SLOT };

	public EquipmentUtils(DreamScript cs) {
		super(cs);
	}

	public void putOnGear(String[] gear) throws InterruptedException {
		for (String item : gear) {
			if (!cs.theTabs.Equipment.isWearing(item)) {
				cs.log("Putting on " + item);
				cs.log(cs.getInventoryUtils().doInventory(item, null, true));
			}
		}
	}

	public void putOnGearReflection(String[] gear) throws InterruptedException {
		for (String item : gear) {
			if (!isWearing(item)) {
				cs.log("Putting on " + item);
				cs.log(cs.getInventoryUtils().doInventory(item, null, true));
			}
		}
	}

	public int getEquipment(int index) {
		return getEquipment(index, cs.getMyPlayer());
	}

	public int getEquipment(int index, Player player) {
		try {
			Object playerInstance = player.getEntityInstance();

			for (Field f1 : playerInstance.getClass().getDeclaredFields()) {
				if (!Modifier.isStatic(f1.getModifiers())
						&& !f1.getType().toString().matches("(int|boolean|byte|.*String)")) {

					f1.setAccessible(true);
					Object playerDef = f1.get(playerInstance);
					for (Field f2 : playerDef.getClass().getDeclaredFields()) {
						if (Modifier.isPrivate(f2.getModifiers()) && f2.getType().toString().equals("class [I")) {

							f2.setAccessible(true);
							int[] equipmentModeled = (int[]) f2.get(playerDef);
							return 0x3fffffff & equipmentModeled[index];
						}
					}
					break;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public String[] getAllGear() {
		List<String> gear = new ArrayList<String>();

		for (int slot : equipment) {
			int id = getEquipment(slot, cs.getMyPlayer());

			if (id > 0) {
				LootItem item = masterLootTable.get(id);
				if (item != null) {
					gear.add(item.getName());
				} else {
					gear.add("null");
				}
			} else {
				gear.add("null");
			}
		}

		return gear.toArray(new String[gear.size()]);
	}

	public boolean isWearing(ItemCondition equipment) {
		for (String gear : getAllGear()) {
			if (equipment.validItem(gear)) {
				return true;
			}
		}

		return false;
	}

	public boolean isWearing(String equipment) {
		for (String gear : getAllGear()) {
			if (gear.equalsIgnoreCase(equipment)) {
				return true;
			}
		}

		return false;
	}

}
